<?php
/**
 * 默认控制器
 * 
 * @version $Id: Site.php 486 2012-11-05 07:19:04Z linsir123 $
 */
class controllerSite extends AppController 
{
	/**
	 * 默认的控制器名称
	 */
	public $defaultAction = 'home';
	
	/**
	 * 首页
	 */
	public function actionHome()
	{
		if (Request::isRobot()) 
			View::display('home/index');
		else 
			if ($this->checkCache('home/index')) 
				return ;
				
		///
		View::assignSet(array(
			'arts' => file_get_contents("http://www.e35go.com/__wordpress/index-posts.php"),
			'taobaokes' => M::item()->getPHome(),
			'shops' => App::O('_home.shop'),
			'dh' => App::O('_homeClass'),
		));
		
		///
		AppView::addCss('themes/01/home.css');
	}
	
	/**
	 * 首页商铺列表
	 */
	public function actionHomeShops()
	{
		$i = intval($_GET['i']);
		if ($this->checkCache('home/_shops-div', $i)) 
			return ;
			
		///
		$shops = App::O('_home.shop');
		if ( ! array_key_exists($i, $shops)) 
			lpUrl::_r();
			
		///
		View::assignSet(array(
			'homeShopsIndex'=>$i,
			'homeShops'=>$shops[$i][1]
		));
	}
	
	/**
	 * FAQ
	 */
	public function actionFaq()
	{
		$m = M::faq();
		$type = intval($_GET['type']);
		if ( ! array_key_exists($type, $m->types)) 
			$type = 0;
			
		///
		if ($this->checkCache('faq/index', $type)) 
			return ;
			
		///
		View::assignSet(array(
			'type' => $type,
			'types' => $m->types,
			'title' => $m->types[$type],
			'list' => $m->getList($type)
		));
		
		///
		AppView::addCss('themes/01/faq.css');
	}
	
	/**
	 * 搜索
	 */
	public function actionSearch()
	{
		if (Request::isPost()) {
			$type = intval($_POST['t']);
			$keyword = trim($_POST['k']);
			switch ($type) {
				/// 商品
				case 0:
				case 1:
					if (strpos($keyword, 'http')===0) {
						$tmp = parse_url($keyword);
						parse_str($tmp['query'], $tmp);
						if ( ! empty($tmp['id'])) {
							$params = array('iid'=>$tmp['id']);
							$url = Route::urlItem($params);
						}
					}
					if (empty($url)) {
						$params = array(
							'pinyin'=>'search', 
							'keyword'=>$keyword,
							'start_price'=>intval($_POST['sp']),
							'end_price'=>intval($_POST['ep']),
							'type'=>($type==0?'list':'zhe')
						);
						$url = Route::urlClass($params);
					}
					break;
					
				/// 店铺
				case 2:
					$url = Route::urlShop(array('nick'=>$keyword));
					break;
			}
		} else 
			$url = lpUrl::__();
		
		///
		lpUrl::redirect($url);
	}
	
	/**
	 * 
	 */
	public function actionChange()
	{
		View::display('change/index');
	}
	
	/**
	 * 
	 */
	public function actionRedirect()
	{
		$data = trim($_GET['data']);
		$action = trim($_GET['action']);
		if (empty($action) || empty($data)) 
			$this->miss();
			
		///
		$url = null;
		switch ($action) {
			case 'channel':
				$list = App::O('_nav.header');
				$url = isset($list[$data])?$list[$data][1]:'';
				break;
		}
		
		///
		if ( ! empty($url)) 
			lpUrl::redirect($url);
		else 
			$this->miss();
	}
	
	/**
	 * 404
	 */
	public function actionMiss()
	{
		$this->miss();
	}
}
